﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using System.Web.Http.Description;
using CPService.Models;

namespace CPService.Controllers
{
    public class InformationByUsersController : ApiController
    {
        private CPServiceContext db = new CPServiceContext();

        // GET: api/InformationByUsers
        public IQueryable<InformationByUser> GetInformationByUsers()
        {
            return db.InformationByUsers;
        }

        // GET: api/InformationByUsers/5
        [ResponseType(typeof(InformationByUser))]
        public IHttpActionResult GetInformationByUser(int id)
        {
            InformationByUser informationByUser = db.InformationByUsers.Find(id);
            if (informationByUser == null)
            {
                return NotFound();
            }

            return Ok(informationByUser);
        }

        // PUT: api/InformationByUsers/5
        [ResponseType(typeof(void))]
        public IHttpActionResult PutInformationByUser(int id, InformationByUser informationByUser)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            if (id != informationByUser.InformationByUserId)
            {
                return BadRequest();
            }

            db.Entry(informationByUser).State = EntityState.Modified;

            try
            {
                db.SaveChanges();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!InformationByUserExists(id))
                {
                    return NotFound();
                }
                else
                {
                    throw;
                }
            }

            return StatusCode(HttpStatusCode.NoContent);
        }

        // POST: api/InformationByUsers
        [ResponseType(typeof(InformationByUser))]
        public IHttpActionResult PostInformationByUser(InformationByUser informationByUser)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            db.InformationByUsers.Add(informationByUser);
            db.SaveChanges();

            return CreatedAtRoute("DefaultApi", new { id = informationByUser.InformationByUserId }, informationByUser);
        }

        // DELETE: api/InformationByUsers/5
        [ResponseType(typeof(InformationByUser))]
        public IHttpActionResult DeleteInformationByUser(int id)
        {
            InformationByUser informationByUser = db.InformationByUsers.Find(id);
            if (informationByUser == null)
            {
                return NotFound();
            }

            db.InformationByUsers.Remove(informationByUser);
            db.SaveChanges();

            return Ok(informationByUser);
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                db.Dispose();
            }
            base.Dispose(disposing);
        }

        private bool InformationByUserExists(int id)
        {
            return db.InformationByUsers.Count(e => e.InformationByUserId == id) > 0;
        }
    }
}